# core/api_client.py
import requests

class APIClient:
    def __init__(self, base_url, token):
        self.base_url = base_url
        #self.token = token
        self.session = requests.Session()
        self.session.headers.update({
            "Content-Type": "application/x-www-form-urlencoded",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
        })


    def query_mask_code(self, city_adcode, prefixes, masks):
        """查询掩码号码"""
        print(f"查询掩码号码，城市编码：{city_adcode}，前缀：{prefixes}，掩码：{masks}")
        url = f"{self.base_url}/app/QueryMaskCode?token="
        params = {
            "city": city_adcode,
            "prefix": prefixes,
            "mask": masks
        }
        
        try:
            response = self.session.post(url, data=params, timeout=10)
            response.raise_for_status()
            return response
        except requests.exceptions.RequestException as e:
            print(f"API请求错误: {e}")
            return {"code": 500, "message": f"请求失败: {str(e)}"}

    def query_shuffle_code(self, city_adcode, prefixes, count=10):
        """查询随机号码"""
        url = f"{self.base_url}/app/QueryShuffleCode"
        params = {
            "token": self.token,
            "city": city_adcode,
            "prefix": prefixes,
            "count": count
        }
        
        try:
            response = self.session.post(url, data=params, timeout=10)
            response.raise_for_status()
            return response.json()
        except requests.exceptions.RequestException as e:
            print(f"API请求错误: {e}")
            return {"code": 500, "message": f"请求失败: {str(e)}"}    